﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Scenario;
using TMPro;
using UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UI
{
    public enum ELittleGameType
    {
        七巧板,
        魔方,
        上锁日记,
    }
    public class UI_LittleGamePanel : MonoBehaviour
    {
        private static UI_LittleGamePanel _instance;

        public GameObject m_lockNotebookPrefab;
        public GameObject m_magicCubePrefab;
        public GameObject m_qiQiaoPrefab;

        private Dictionary<ELittleGameType, GameObject> m_gameObjDict;
        private GameObject m_lastGameObj;

        private void Awake()
        {
            _instance = this;

            m_gameObjDict = new Dictionary<ELittleGameType, GameObject>();
            
            transform.Find("CloseBtn").GetComponent<Button>().onClick.AddListener(Hide);
        }
        private void OnEnable()
        {
            PlayerController.SetPause(true);
        }
        private void OnDisable()
        {
            PlayerController.SetPause(false);
        }

        private void StartGameInternal(ELittleGameType type)
        {
            if (!m_gameObjDict.TryGetValue(type, out m_lastGameObj))
            {
                switch (type)
                {
                    case ELittleGameType.七巧板:
                        m_lastGameObj = GameObject.Instantiate(m_qiQiaoPrefab, transform);
                        break;
                    case ELittleGameType.魔方:
                        m_lastGameObj = GameObject.Instantiate(m_magicCubePrefab, transform);
                        break;
                    case ELittleGameType.上锁日记:
                        m_lastGameObj = GameObject.Instantiate(m_lockNotebookPrefab, transform);
                        break;
                }
                m_gameObjDict.Add(type, m_lastGameObj);
            }
            else
            {
                m_lastGameObj.SetActive(true);
            }
        }
        private void Hide()
        {
            if (m_lastGameObj != null)
            {
                m_lastGameObj.SetActive(false);
                m_lastGameObj = null;
            }
            _instance.gameObject.SetActive(false);
        }
        
        public static void StartGame(ELittleGameType type)
        {
            if (!_instance.gameObject.activeSelf) _instance.gameObject.SetActive(true);
            _instance.StartGameInternal(type);
        }
        public static void Pass()
        {
            if (!_instance.gameObject.activeSelf) return;
            _instance.Hide();
        }
    }
}
